package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.GetAdvertByUidRes;
import com.hq.fxh.R;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class ToBusinessTempActivity extends BaseActivity {

    private static final int AD_EDIT = 10;
    private ImageView back;
    private TextView ok;
    private RecyclerView recyclerView;
    private int uid;
    private BusinessAdAdapter adapter;
    private List<AdBannerInfo> busList;
    private List<AdBannerInfo> bottomList = new ArrayList<>();
    private List<AdBannerInfo> topList = new ArrayList<>();
    private boolean isBottom;
    List<AdBannerInfo> currentData = new ArrayList<>();
    private int topCount;
//    private int bottomCount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_to_business_temp;
    }

    @Override
    protected void initView() {
        back = (ImageView) findViewById(R.id.back);
        ok = (TextView) findViewById(R.id.ok);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    @Override
    protected void initData() {
        uid = SharedPreferenceUtils.getMemberId(ToBusinessTempActivity.this);
        isBottom = getIntent().getBooleanExtra("isbottom", true);
        topCount = getIntent().getIntExtra("topCount", 0);
//        bottomCount = getIntent().getIntExtra("bottomCount", 0);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(ToBusinessTempActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);

        UserServiceImpl.getEnterpriseAdvert(uid + "", new NetworkSubscriber<GetAdvertByUidRes>() {
            @Override
            public int onSuccess(GetAdvertByUidRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    currentData.clear();
                    if (isBottom) {
                        currentData.addAll(data.getData().getBottom());
                    } else {
                        currentData.addAll(data.getData().getTop());
                    }
                    if (adapter == null) {
                        adapter = new BusinessAdAdapter(ToBusinessTempActivity.this, currentData);
                        recyclerView.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtils.show(ToBusinessTempActivity.this, data.getMsg());
                }
                return 0;
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomList.clear();
                topList.clear();
                if (adapter != null) {
                    busList = adapter.getBussList();
                }
                if (busList != null && busList.size() > 0) {
                    for (AdBannerInfo bean : busList) {
                        if (bean.getAdvert_postion() == 1) {
                            bottomList.add(bean);//1底部
                        } else {
                            topList.add(bean);
                        }
                    }
//                    if ((bottomCount + bottomList.size()) > 10) {
//                        ToastUtils.show(ToBusinessTempActivity.this, "最多再添加" + (10 - bottomCount) + "个广告模板");
//                        return;
//                    }
                    if ((topCount + topList.size()) > 10) {
                        ToastUtils.show(ToBusinessTempActivity.this, "最多再添加" + (10 - topCount) + "个广告模板");
                        return;
                    }
                }

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putBoolean("isbottom", isBottom);
                bundle.putSerializable("topList", (Serializable) topList);
                bundle.putSerializable("bottomList", (Serializable) bottomList);
                intent.putExtras(bundle);
                setResult(AD_EDIT, intent);
                finish();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(this, "企业模板");
    }

    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(this, "企业模板");
    }
}
